<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2022-03-26 22:10:32
 */

namespace app\admin\model;
use app\common\model\BaseModel;

class SystemRule extends BaseModel {
	// 设置数据表（不含前缀）
	//protected $name = '';

	// 定义时间戳字段名
	protected $createTime = '';
	protected $updateTime = '';

	public function getTree($map) {
		$data = $this->where($map)->order('vieworder asc, id asc')->select();
		$data = $data->toArray(); //转换为数组
		//print_r($this->getLastSql());
		return $this->parseTree($data);
	}

	private function parseTree($data) {
		$isopen_config = config('engineeconfig.isopen_config');
		$yes_config = config('engineeconfig.yes_config');

		$parents = $childs = $subs = array();
		$totalNum = 0;
		foreach ($data as $key => $value) {
			$value['state_show'] = $isopen_config[$value['state']];
			$value['display_show'] = $yes_config[$value['display']];
			$value['is_selfmenu_show'] = $yes_config[$value['is_selfmenu']];

			if ($value['parentid']) {
				if ($value['level'] == 3) {
					$subs[$value['parentid']][$value['id']] = $value;
				} else if ($value['level'] == 2) {
					$childs[$value['parentid']][$value['id']] = $value;
				} else {
					$others[][$value['id']] = $value;
				}
			} else {
				$parents[$value['id']] = $value;
			}
			$totalNum++;
		}
		//print_r($parents);print_r($childs);print_r($subs);exit;
		//print_r($others);

		foreach ($childs as $key => $value) {
			foreach ($value as $key2 => $value2) {
				if (isset($subs[$key2])) {
					$childs[$key][$key2]['hassub'] = 1;
					$childs[$key][$key2]['subs'] = $subs[$key2];
				} else {
					$childs[$key][$key2]['hassub'] = 0;
					$childs[$key][$key2]['subs'] = [];
				}
			}
		}
		
		foreach ($parents as $key => $value) {
			//$parents[$key]['ischild'] = $childs[$key];
			if (isset($childs[$key])) {
				$parents[$key]['hassub'] = 1;
				$parents[$key]['childs'] = $childs[$key];
			} else {
				$parents[$key]['hassub'] = 0;
				$parents[$key]['childs'] = [];
			}
		}
		//print_r($parents);exit;
		
		//$others && $parents += $others;

		return array($parents, $totalNum);
	}

	public function getParentTree() {
		$map = [
			['is_parent', '=', 1]
		];
		$data = $this->where($map)->order('vieworder desc')->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseTree($data);
	}

	public function getParentList() {
		$map = [
			['is_parent', '=', 1]
		];
		$data = $this->where($map)->order('vieworder desc')->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}
	
	public function getParentListByCid($cid) {
		$map = [
			['cid', '=', $cid],
			['is_parent', '=', 1]
		];
		$data = $this->where($map)->order('vieworder desc')->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	/**
	 * 菜单树状结构集合
	 */
	public function getMenuTree($cateId, $groupid) {
		$data = $this->get_tree($cateId, $groupid, 0, 0);
		//print_r($data);
		return $data;
	}

	//取得树形结构的菜单
	public function get_tree($cateId=0, $groupid=0, $myid=0, $parent="", $Level=1) {
		//print_r($groupid);
		$data = $this->admin_menu($cateId, $myid, $groupid);
		// echo $myid.'<br>';
		//print_r($data);
		//echo '<br>';
		// if ($Level == 2) {
		// 	exit;
		// }
		$retData = [];
		$Level++;
		if (is_array($data)) {
			foreach ($data as $key => $value) {
				$id = $value['id'];
				//$model = ucwords($a['model']);
				//$controller = ucwords($a['controller']);
				$module = $value['module'];
				$controller = $value['controller'];
				$action = $value['action'];
				//附带参数
				$fu = "";
				// if ($a['data']) {
				// 	$fu = "?" . $a['data'];
				// }
				$array = array(
					"icon"	 => $value['icon'],
					"id"	 => $id,
					"name"	 => $value['name'],
					"parent" => $parent,
					"controller" => $controller,
					//"navFlag" => $a['nav_flag'],
					//'sideFlag' => strtolower($module).'_'.strtolower($controller),
					'sideFlag' => $module.'_'.$controller,
					//"btn_class" => $value['btn_class'] ? $value['btn_class'] : 'btn-default',
					//"color"		=> $a['color'],
					"url"	 => url("{$module}/{$controller}/{$action}", ['menucid'=>$value['cid']]),
					"display"=> $value['display'],
					//'is_my_menu' => $a['is_my_menu']
				);

				$retData[$id] = $array;

				if ($value['has_sub']) {
					$child = $this->get_tree($cateId, $groupid, $value['id'], $id, $Level);
					//print_r($child);exit;
					if ($child) {
						$array['url'] = '';
					}
					//由于后台管理界面只支持三层，超出的不层级的不显示
					if ($child && $Level <= 2) {
						$retData[$id]['items'] = $child;
					}
				}
			}
			return $retData;
		}
		return false;
	}

	public function countSearch($map) {
		return $this->where($map)->count();
	}

	public function search($map, $limitStr="0,0", $orderBy='') {
		list($offset, $limit) = explode(",", $limitStr);
		$data = $this->where($map)->order('create_time desc')->limit($offset, $limit)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	protected function parseSearch($data) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('engineeconfig.isopen_config');

		$list = array();
		foreach ($data as $key => $value) {
			//通用解析
			//$value['create_time_show'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';
			//$value['state_show'] = isset($value['state']) ? $isopen_config[$value['state']] : '';
			//$value['picurl'] = $value['picurl'] ? $ftpWeb.$value['picurl'] : '';
			//通用解析

			$list[$value['id']] = $value;
		}
		return array($list);
	}

	protected function parseInfo($info) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('engineeconfig.isopen_config');

		//通用解析
		//$info['create_time_show'] = $info['create_time'] ? date("Y-m-d H:i:s", $info['create_time']) : '';
		//$info['state_show'] = isset($info['state']) ? $isopen_config[$info['state']] : '';
		//$info['picurl'] = $info['picurl'] ? $ftpWeb.$info['picurl'] : '';
		//通用解析

		return $info;
	}

}